﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gqq.Csharpindepth.DataStructure
{
    public class BubbleSortClass
    {
        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="arr"></param>
        public static void BubbleSort(int[] arr)
        {
            for (int i = 0; i < arr.Length - 1; ++i)
            {
                bool isChange = false;
                //这样一个循环，
                for (int j = 0; j < arr.Length - 1 - i; ++j)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        var temp = arr[j + 1];
                        arr[j + 1] = arr[j];
                        arr[j] = temp;
                        isChange = true;
                    }
                }
                foreach (int i1 in arr)
                {
                    Console.Write("{0}\t", i1);
                }
                Console.WriteLine();
                if (!isChange) break;
            }
        }
    }
}
